package com.handa.documentManager.controller;


import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.handa.common.config.SnowflakeConfig;
import com.handa.common.entity.Response;
import com.handa.documentManager.entity.DO.DocumentUserBucketDO;
import com.handa.documentManager.service.DocumentUserBucketService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (DocumentUserBucket)表控制层
 *
 * @author makejava
 * @since 2025-09-14 21:08:34
 */
@RestController
@RequestMapping("/documentManager/documentUserBucket")
public class DocumentUserBucketController {
    /**
     * 服务对象
     */
    @Resource
    private DocumentUserBucketService documentUserBucketService;

    @Resource
    private SnowflakeConfig snowflakeConfig;

    /**
     * 查询所有数据
     */
    @PostMapping("getAll")
    public Response<?> selectAll(@RequestBody DocumentUserBucketDO documentUserBucketDO) {
        return Response.success(this.documentUserBucketService.list(new QueryWrapper<>(documentUserBucketDO)));
    }

    /**
     * 查询单条数据
     */
    @GetMapping("getByUserId")
    public Response<?> getByUserId(@RequestParam String userId) {
        DocumentUserBucketDO documentUserBucket = documentUserBucketService.getDocumentUserBucket(userId);
        return Response.success(documentUserBucket);
    }

    @PostMapping("saveOrUpdate")
    public Response<?> saveOrUpdate(@RequestBody DocumentUserBucketDO documentUserBucketDO) {
        Assert.notNull(documentUserBucketDO.getUserId(), "用户Id 不能为空");
        Assert.notBlank(documentUserBucketDO.getBucketName(), "文件桶名不能为空");

        if (documentUserBucketDO.getId() == null) {
            documentUserBucketDO.setId(snowflakeConfig.snowflakeId());
        }
        documentUserBucketService.saveOrUpdate(documentUserBucketDO);
        return Response.success();
    }

    /**
     * 删除数据
     */
    @PostMapping("delete")
    public Response<?> delete(@RequestBody List<Long> idList) {
        return Response.success(this.documentUserBucketService.removeByIds(idList));
    }
}

